package cn.joyfulmedia.mis.service;

import cn.joyfulmedia.common.util.page.Pagination;
import cn.joyfulmedia.dao.ExamMisAccountMapper;
import cn.joyfulmedia.entity.ExamMisAccount;
import cn.joyfulmedia.mis.service.interfaces.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author darren.dai
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private ExamMisAccountMapper examMisAccountMapper;


    @Override
    public Integer saveAccount(ExamMisAccount entity) throws Exception {
        Integer i = examMisAccountMapper.saveAccount(entity);
        if (entity.getId() != null) {
            return entity.getId();
        }
        return i;
    }

    @Override
    public Integer updateAccountById(ExamMisAccount entity) throws Exception {
        return examMisAccountMapper.updateAccountById(entity);
    }

    /**
     * 删除公司用户
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Integer delUser(Integer id) throws Exception {
        return examMisAccountMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ExamMisAccount selectAccountById(Long id) throws Exception {
        return examMisAccountMapper.selectAccountById(id);
    }

    @Override
    public ExamMisAccount selectByUsername(String username) {
        return examMisAccountMapper.selectByUsername(username);
    }

    public Integer listPageCount(ExamMisAccount account) {
        return this.examMisAccountMapper.listPageCount(account);
    }

    @Override
    public List<ExamMisAccount> listPage(ExamMisAccount account, Pagination pagination) {
        List<ExamMisAccount> list = new ArrayList<ExamMisAccount>();
        int count = this.listPageCount(account);
        if (count > 0) {
            pagination.setTotal(count);
            list = examMisAccountMapper.listPage(account, pagination);
        }
        return list;
    }

    @Override
    public Set<String> findPermissions(String username) {
        return examMisAccountMapper.findPermissions(username);
    }
}
